package com.runa.monitor.platinterface.extend.nb.service;

import com.iotplatform.client.NorthApiClient;
import com.iotplatform.client.NorthApiException;
import com.iotplatform.client.dto.ClientInfo;
import com.iotplatform.client.invokeapi.Authentication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OldNbiotService {


    @Value("${nbiot.platformIp}")
    public String platformIp;
    @Value("${nbiot.platformPort}")
    public String platformPort;
    @Value("${nbiot.appId}")
    public String appId;
    @Value("${nbiot.secret}")
    public String secret;

    NorthApiClient northApiClient = null;

    public NorthApiClient getClient() {
        if (this.northApiClient != null) {
            return northApiClient;
        }
        ClientInfo clientInfo = new ClientInfo();
        clientInfo.setPlatformIp(platformIp);
        clientInfo.setPlatformPort(platformPort);
        clientInfo.setAppId(appId);
        clientInfo.setSecret(secret);
        this.northApiClient = new NorthApiClient();
        try {
            this.northApiClient.setClientInfo(clientInfo);
            this.northApiClient.initSSLConfig();
        } catch (Exception e) {
            log.error("", e);
        }
        Authentication authentication = new Authentication(northApiClient);
        try {
            authentication.startRefreshTokenTimer();
        } catch (NorthApiException e) {
            log.error("", e);
        }
        return this.northApiClient;
    }
}
